﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CokeZeroCodeUITest
{
    using System;
    using System.Collections;
    using System.Net.Mail;
    using System.Net;
    using System.Configuration;
    using System.Xml;

    public class SendMail
    {
        private ArrayList attachments = new ArrayList();
        private System.Net.Mail.MailMessage mail;
        private String smtpServer;
        private String sendTo;
        private String sendCC;
        private String sentFrom;
        private String subject;
        private String body;

        /// <summary>
        /// Constructor to send mail
        /// </summary>
        public SendMail()
        {
            this.smtpServer = "smtphost";
            this.mail = new System.Net.Mail.MailMessage();
        }

        public void MailProperties(String from, String to, String cc, String subject, String body)
        {
            System.Text.StringBuilder bodyBuilder = new System.Text.StringBuilder();
            bodyBuilder.Append("*** Active user sending this mail is " + System.Environment.UserName + " ***\r\n\r\n");
            bodyBuilder.Append(body);

            this.sendTo = to;
            this.sendCC = cc;
            this.sentFrom = from;
            this.subject = subject;
            this.body = bodyBuilder.ToString();
        }

        /// <summary>
        /// Values to and subject will come from either the config file or environment variables
        /// from is the logged in user
        /// </summary>
        /// <param name="body">The body of the email</param>
        public void MailProperties(String body)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Config.xml");
            XmlNode FromNode = doc.SelectSingleNode("root/EmailFrom");
            XmlNode ToNode = doc.SelectSingleNode("root/EmailTo");
            XmlNode TitleNode = doc.SelectSingleNode("root/EmailTitle");

            this.sentFrom = FromNode.InnerText;
            this.sendTo = ToNode.InnerText;
            this.sendCC = "";
            this.subject = TitleNode.InnerText;
            this.mail.IsBodyHtml = true;
            this.body = body;
        }

        /// <summary>
        /// Once you have set all the properties, then you can send you message using this method
        /// </summary>
        /// <returns>true if there were no errors</returns>
        public bool Send()
        {
            bool sent = false;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.From = new MailAddress(this.sentFrom);

            string[] receivers = sendTo.Split(';');
            foreach (string alias in receivers)
            {
                mail.To.Add(new MailAddress(alias));
            }


            if (!string.IsNullOrEmpty(this.sendCC))
            {
                receivers = sendCC.Split(';');
                foreach (string alias in receivers)
                {
                    mail.CC.Add(new MailAddress(alias));
                }
            }
            mail.Body = this.body;
            mail.Subject = this.subject;
            SmtpClient smtpClient = new SmtpClient(this.smtpServer);

            try
            {
                // Add credentials if the SMTP server requires them.
                smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                smtpClient.Send(this.mail);

                sent = true;
            }
            catch (Exception anyException)
            {
                //trace.WriteLine("unable to send mail. reason:");
                //trace.WriteLine(anyException.ToString());
            }

            return sent;
        }

        /// <summary>
        /// Allows you to specify a different server to send email from
        /// </summary>
        public String SmtpServer
        {
            get { return this.smtpServer; }
            set { this.smtpServer = value; }
        }
    }
}
